package com.bang.itstack.day13;

/**
 * @ClassName: AbstractLogger
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2021/10/22 17:27
 * @Version: 1.0
 * @Modified By:
 */
public abstract class AbstractLogger {

    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;
    protected int level;

    //责任链中的下一个元素
    protected AbstractLogger nextLogger;

    public AbstractLogger(int level) {
        this.level = level;
    }

    public void setNextLogger(AbstractLogger nextLogger) {
        this.nextLogger = nextLogger;
    }

    private void logMessage(int level, String message) {
        if (this.level <= level) {
            write(message);
        }
        if (nextLogger != null) {
            nextLogger.logMessage(level, message);
        }
    }

    abstract protected void write(String message);

    public void info(String message) {
        logMessage(INFO, message);
    }

    public void debug(String message) {
        logMessage(DEBUG, message);
    }

    public void error(String message) {
        logMessage(ERROR, message);
    }
}
